import type { UserConfig, ConfigEnv } from 'vite';
import { loadEnv } from 'vite';
import * as Path from 'path';
import { parseEnv } from './build/utils';
import { createVitePlugins } from './build/vite/plugin';
const resolvePaths = (path: string) => {
  return Path.resolve(__dirname, path);
};

export default ({ command, mode }: ConfigEnv): UserConfig => {
  const isBuild = command === 'build';
  const root = process.cwd();
  const env = loadEnv(mode, root);
  const viteEnv = parseEnv(env);
  const { VITE_APP_TITLE, VITE_DROP_CONSOLE, VITE_PUBLIC_PATH, VITE_USE_MOCK } = viteEnv;
  return {
    base: VITE_PUBLIC_PATH,
    root,
    resolve: {
      alias: {
        '@': resolvePaths('./src'),
      },
    },
    esbuild: {
      pure: VITE_DROP_CONSOLE ? ['console.log', 'debugger'] : [],
    },
    plugins: createVitePlugins(viteEnv, isBuild),
    build: {
      rollupOptions: {
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
        },
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          charset: false,
        },
      },
    },
  };
};
